package hl.demosample.ui.fragment.girlclassify

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.kingja.loadsir.core.LoadService
import hl.demosample.R
import hl.demosample.app.appViewModel
import hl.demosample.app.base.BaseFragment
import hl.demosample.app.ext.*
import hl.demosample.app.weight.recycleview.SpaceItemDecoration
import hl.demosample.data.model.bean.GirlClassifyData
import hl.demosample.databinding.FragmentGirlClassfyBinding
import hl.demosample.ui.adapter.GirlClassifyAdapter
import hl.demosample.viewmodel.request.RequestGirlClassifyViewModel
import hl.demosample.viewmodel.state.GirlClassifyViewModel
import kotlinx.android.synthetic.main.include_smart_list.*
import kotlinx.android.synthetic.main.include_smart_rv.*
import kotlinx.android.synthetic.main.include_toolbar.*
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

/*
* path: DemoSample 
* Author: hl
* Create: 2021-06-29 17:55
* Changes (from 2021-06-29)
* Desc:
*/
class GirlClassifyFragment : BaseFragment<GirlClassifyViewModel, FragmentGirlClassfyBinding>() {

    private var pageNum = 1

    private val pageSize = 20

    private val mGirl="Girl"

    override fun layoutId() = R.layout.fragment_girl_classfy

    private var listData = ArrayList<GirlClassifyData.DataBean>()

    private lateinit var girlClassifyAdapter: GirlClassifyAdapter

    /**数据层ViewModel**/
    private val requestGirlClassifyViewModel: RequestGirlClassifyViewModel by viewModels()

    /**界面状态管理者**/
    private lateinit var loadsir: LoadService<Any>

    override fun initView(savedInstanceState: Bundle?) {
        toolbar.initClose("分类Girl") {
            nav().navigateUp()
        }
        girlClassifyAdapter = GirlClassifyAdapter(arrayListOf())
        //状态页配置
        loadsir = loadServiceInit(listSmartRefresh) {
            //点击重试时触发的操作
            loadsir.showLoading()
            requestGirlClassifyViewModel.girlClassify(mGirl, mGirl, pageNum, pageSize)
        }
        recyclerView.init(LinearLayoutManager(context), girlClassifyAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, ConvertUtils.dp2px(5f)))
            //初始化FloatingActionButton
            it.initFloatBtn(floatbtn, false)
        }

        listSmartRefresh.refresh {
            pageNum = 1
            requestGirlClassifyViewModel.girlClassify(mGirl, mGirl, pageNum, pageSize)
            listSmartRefresh.finishRefresh()
        }.loadMore {
            requestGirlClassifyViewModel.girlClassify(mGirl, mGirl, pageNum, pageSize)
            listSmartRefresh.finishLoadMore()
        }

        girlClassifyAdapter.run {
            setOnItemClickListener { adapter, view, position ->
                nav().navigateAction(R.id.action_mainfragment_to_photoViewFragment, Bundle().apply {
                    putString("url",girlClassifyAdapter.data[position].url)
                })
            }
        }
    }

    override fun lazyLoadData() {
        requestGirlClassifyViewModel.girlClassify(mGirl, mGirl, pageNum, pageSize)
    }

    override fun createObserver() {
        requestGirlClassifyViewModel.mData.observe(viewLifecycleOwner, Observer {
            if (pageNum == 1) {
                listData.clear()
            }
            listData.addAll(it.listData ?: emptyList())
            if (it.listData?.size ?: 0 >= pageSize) {
                pageNum++
            } else {
                listSmartRefresh.setNoMoreData(true)
            }
            if (listData.isNotEmpty() && listData.size > 0) {
                loadsir.showSuccess()
                girlClassifyAdapter.setNewInstance(listData)
            } else {
                loadsir.showEmpty()
            }
        })

        appViewModel.run {
            //监听全局的列表动画改编
            appAnimation.observeInFragment(this@GirlClassifyFragment) {
                girlClassifyAdapter.setAdapterAnimation(it)
            }
        }
    }
}